import { message } from 'antd';
import { json, save, dele, echo, role } from '../services/users';

let reqPara = {}; // 存放记录查询参数

export default {
  namespace: 'users',

  // 状态机
  state: {
    loading: false, // 表格是否显示转圈
    data: [], // 表格数据
    total: 0, // 表格记录条数
    noSelect: true, // 是否选择了记录
    selectOne: false, // 是否只选择了一条记录
    selectedRowKeys: [], // 存放已选择记录的id
    gridSel_rows: [], // 存放已选择记录的全部行属性
    dialog_visible: false, // 对话框是否显示
    dialog_loading: false, // 对话框的保存按钮是否显示转圈
    roleArr: [], // 存放角色数组
    echoModel: [], // 存放编辑回显model
  },

  // 发起网络请求
  effects: {
    *submit({ payload }, { call, put }) {
      const response = yield call(json, payload);
      reqPara = payload;
      yield put({ type: 'callback', response });
    },
    *save({ payload }, { call, put }) {
      let response = yield call(save, payload);
      yield put({ type: 'callback_save', response, payload });
      response = yield call(json, reqPara);
      yield put({ type: 'callback', response });
    },
    *dele({ payload }, { call, put }) {
      let response = yield call(dele, payload);
      yield put({ type: 'callback_dele', response });
      response = yield call(json, reqPara);
      yield put({ type: 'callback', response });
    },
    *echo({ payload }, { call, put }) {
      const response = yield call(echo, payload);
      yield put({ type: 'callbackEcho', response });
    },
    *role({ payload }, { call, put }) {
      const response = yield call(role, payload);
      yield put({ type: 'callback_role', response });
    },
  },

  // 回调处理
  reducers: {
    callback(st, { response }) {
      const state = st;
      state.loading = false;
      if (response.success) {
        state.data = response.root;
        state.total = response.total;
      } else if (response.status === 1) message.error(response.msg);
      return { ...state };
    },
    callback_dele(st, { response }) {
      const state = st;
      state.loading = false;
      if (response.success) {
        message.success(response.msg);
        state.noSelect = true;
        state.selectOne = false;
        state.selectedRowKeys = [];
        state.gridSel_rows = [];
      } else if (response.status === 1) message.error(response.msg);
      return { ...state };
    },
    callback_save(st, { response, payload }) {
      const state = st;
      state.dialog_loading = false;
      if (response.success) {
        message.success(response.msg);

        state.dialog_visible = false;

        if (payload.action === 'edit') {
          // 更新gridSel_rows
          state.gridSel_rows[0] = { ...state.gridSel_rows[0], ...payload.model };
        }
      } else if (response.status === 1) message.error(response.msg);
      return { ...state };
    },
    callbackEcho(st, { response }) {
      const state = st;
      if (response.success) {
        state.echoModel = response.model;
      } else message.warn(response.msg);
      return { ...state };
    },
    callback_role(st, { response }) {
      const state = st;
      if (response.success) {
        state.roleArr = response.root;
      } else message.warn(response.msg);
      return { ...state };
    },
  },
};
